package com.china.soft.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china.soft.common.model.EnvironmentConfig;

/**
 * @desc web 前端应用应用配置测试
 * @class com.china.soft.web.controller.TestConfigController
 * @author suan@chinasoftinc.com
 * @version V1.0
 */
@RefreshScope
@RestController
public class TestConfigController {

    private static final String APPLICATION_NAME_CONFIG = "spring.application.name";
    private static final String ACTIVE_PROFILE_CONFIG = "spring.profiles.active";
    private static final String LABEL_CONFIG = "spring.cloud.config.label";
    private static final String CONFIG_SERVER_CONFIG = "spring.cloud.config.uri";

    @Autowired
    private Environment env;

    @Value("${appName}")
    private String appName;

    @RequestMapping("/app-name")
    public String getAppName() {
        return appName;
    }

    @RequestMapping(value = "/config/{configName:.*}", method = RequestMethod.GET)
    public String queryConfig(@PathVariable String configName) {
        return env.getProperty(configName, "undefined");
    }

    @RequestMapping(value = "/env", method = RequestMethod.GET)
    public EnvironmentConfig queryEnv() {
        return EnvironmentConfig.builder().application(env.getProperty(APPLICATION_NAME_CONFIG))
                .profiles(env.getProperty(ACTIVE_PROFILE_CONFIG)).label(env.getProperty(LABEL_CONFIG))
                .serverUri(env.getProperty(CONFIG_SERVER_CONFIG)).build();
    }
}
